/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp;

import utils.Email;
import edu.chl.niclun.projectwebshop.nbp.DataBase.Database;
import edu.chl.niclun.projectwebshop.nbp.Entities.Client;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *Controllerbean for the Login.xhtml
 * @author Nume
 */
@ManagedBean(name="LoginControllerBean")
@RequestScoped
public class LoginControllerBean {
    //boolean to determinate if we should navigate
    private boolean navigate = false;
    //instance of loginBacking bean
    @ManagedProperty(value="#{LoginBackingBean}")
    private LoginBackingBean loginBackingBean;
    
    @ManagedProperty(value="#{LoginBean}")
    private LoginBean loginBean;
    /** Creates a new instance of LoginControllerBean */
    public LoginControllerBean() {
    }

    public LoginBackingBean getLoginBackingBean() {
        return loginBackingBean;
    }

    public void setLoginBackingBean(LoginBackingBean loginBackingBean) {
        this.loginBackingBean = loginBackingBean;
    }

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }
   
    public boolean isNavigate() {
        return navigate;
    }

    public void setNavigate(boolean navigate) {
        this.navigate = navigate;
    }
    
    //actionevent method for login. retreives the name(if it exists) from the database and checks the password
    public void logIn(ActionEvent avt){
      List<Client> clientList =  Database.getWebshopController().getClient(loginBackingBean.getLoginName());
       
        for(Client client : clientList){
            if(loginBackingBean.getPassword().equals(client.getPassword())){
                loginBean.setIsLoggedIn(true);
                loginBean.setUser(client);
                navigate = true;
            }
                
        }
        if(!navigate)
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Password or Email was incorrect!"));
        
        
    }
    
    public void forgotPassword(ActionEvent ae) {
        List<Client> clientList =  Database.getWebshopController().getClient(loginBackingBean.getLoginName());
        
        if(clientList.size() > 0) {
            Client client = clientList.get(0);
            String[] email = { client.getEmail() };
            String message = "Login details for " + client.getFname() + " " + client.getLname() + "\n\n" +
                    "Login: " + client.getEmail() + "\nPassword: " + client.getPassword();
            try {
                Email.sendTo(email, message, "Login details");
                loginBean.setUser(client);
                loginBean.setIsLoggedIn(false);
                navigate = true;
            } catch (Exception ex) {
                Logger.getLogger(LoginControllerBean.class.getName()).log(Level.SEVERE, null, ex);
                navigate = false;
            }
        } else {
            navigate = false;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The given email is not registered!"));
        }
    }
    
    //navigate
    public String navigate(){
        if(navigate){
            navigate = false;
            return "index";
        }
        else
            return "login";
    }
    
    public String navigateToRegister() {
        return "register";
    }
    
    public String navigateToForgotPassword() {
        if(navigate)
            return "forgotPassword";
        else
            return "login";
    }
    
}
